﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using funktionsplotter.contracts;
using ZedGraph;

namespace funktionsplotter.gui
{
    public partial class FrmFunktionsplotter : Form, IGui
    {
        public FrmFunktionsplotter()
        {
            InitializeComponent();
        }

        public void Set_Size()
        {
            zg1.GraphPane.YAxis.Cross = 0.0;
            zg1.Location = new Point(20, 90);
            // Leave a small margin around the outside of the control
            zg1.Size = new Size(this.ClientRectangle.Width - 20, this.ClientRectangle.Height - 90);
        }

        private void FrmFunktionsplotter_Load(object sender, EventArgs e)
        {
            Set_Size();
        }

        private void FrmFunktionsplotter_Resize(object sender, EventArgs e)
        {
            Set_Size();
        }

        private void bu_berechne_Click(object sender, EventArgs e)
        {
           Berechnen(new Tuple<string, double, double, int>(tb_Funktion.Text, Double.Parse(tb_MinWert.Text),
                                                                 double.Parse(tb_MaxWert.Text), 100));
        }

        public void Display(IEnumerable<Point> punkte)
        {
            GraphPane myPane = zg1.GraphPane;
            var graphpunkte = new PointPairList( );
            // Achsenbeschriftung
            myPane.Title.Text = "Funktionsplotter";
            myPane.XAxis.Title.Text = "X";
            myPane.YAxis.Title.Text = "Y";

            // Zeichne den Graph anhand der Puntkte Liste
            foreach (var point in punkte)
            {
                graphpunkte.Add(point.X, point.Y);
            }

            LineItem myCurve = myPane.AddCurve("Der Graph", graphpunkte, Color.Red,
                                     SymbolType.None);

            // Calculate the Axis Scale Ranges
            zg1.AxisChange();
        }

        public event Action<Tuple<string, double, double, int>> Berechnen;
    }
}
